﻿<template>
  <a-modal
    :title="title"
    width="600px"
    :visible="visible"
    dialogClass="zero-modal"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <div class="order-type-modal-content">
        <a-form :form="form" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-form-item label="类型编码">
            <a-input placeholder="请输入类型编码" :disabled="type==='edit'" v-decorator="['code',{rules:[{required:true,message:'类型编码不可为空！'}]}]" />
          </a-form-item>
          <a-form-item label="类型名称">
            <a-input placeholder="请输入类型名称" v-decorator="['name',{rules:[{required:true,message:'类型名称不可为空！'}]}]" />
          </a-form-item>
          <a-form-item label="排序">
            <a-input-number placeholder="请输入排序" style="width: 100%" v-decorator="['sort',{rules:[
              {required:true,message:'排序不可为空！'},
              { validator: sortValidate }
            ],validateFirst:true}]" :min="1" />
          </a-form-item>
          <a-form-item label="所属车间" v-if="pObj.id">
            <a-select style="width: 100%" placeholder="请选择所属车间" v-decorator="['lesWorkShopType', {rules: [{required:true,message:'所属车间不可为空！'}]}]">
              <a-select-option v-for="(item,index) in selectOptions1" :key="index" :value="item.code">{{ item.name }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-form>
      </div>
    </a-spin>
    
    <template slot="footer">
      <a-button key="back" @click="handleCancel">取消</a-button>
      <a-button key="submit" type="primary" :loading="confirmLoading" @click="handleSubmit">确认</a-button>
    </template>
  </a-modal>
</template>

<script>
  import {WareOrderTypeAdd,WareOrderTypeEdit} from '@/api/modular/main/WmsOrderTypeManage'
  export default {
    name:'orderTypeModal',
    emits:['update:visible','callback'],
    props:{
      visible:{
        type:Boolean,
        default:false
      },
      type:{
        type:String,
        default:'add'
      },
      obj:{
        type:Object,
        default:function(){
          return {}
        }
      },
      pObj:{
        type:Object,
        default:function(){
          return {}
        }
      }
    },
    watch:{
      visible:{
        immediate:true,
        handler(newV,oldV){
          if (newV!==oldV){
            this.initShow()
          }
        }
      }
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 }
        },
        confirmLoading: false,
        title:'',
        selectOptions1:[],
        form: this.$form.createForm(this)
      }
    },
    methods: {
      initShow(){
        if (this.visible) {
          this.getSelectOptions()
          if (this.type==='add'){
            this.title="新增单据类型"
            let _initForm = {sort:1}
            this.$nextTick(()=>{
              this.form.setFieldsValue(_initForm)
            })
          } else {
            this.title="编辑单据类型"
            let _initForm = {
              name:this.obj.name,
              code:this.obj.code,
              sort:this.obj.sort
            }
            if (this.pObj.id) {
              _initForm.lesWorkShopType = this.obj.lesWorkShopType
            }
            this.$nextTick(()=>{
              this.form.setFieldsValue(_initForm)
            })
          }
        }
      },
      getSelectOptions(){
        if (this.selectOptions1.length<=0) {
          this.selectOptions1 = this.$options.filters['dictData']('les_workshop_type')
        }
      },
      sortValidate(rule, value, callback){
        if (Number(value)!==parseInt(value)) {
          callback('只能是整数');
        } else {
          callback();
        }
      },
      handleSubmit () {
        this.form.validateFields((errors, values) => {
          if (!errors) {
            this.$loading.show()
            this.handleSubmitAjax(values,(f)=>{
              this.$loading.hide()
              if (f) {
                this.$message.success('操作成功！');
                this.handleCancel()
                this.$emit('callback')
              }
            })
          }
        })
      },
      handleSubmitAjax (params,callback){
        if (this.type==='add') {
          this.handleAddAjax(params,callback)
        } else {
          this.handleEditAjax(params,callback)
        }
      },
      handleAddAjax(params,callback){
        let _params = {...params}
        if (!this.pObj.id) {
          _params.pid = 0
        } else {
          _params.pid = this.pObj.id
        }
        WareOrderTypeAdd(_params).then(()=>{
          callback(true)
        }).catch(()=>{
          callback(false)
        })
      },
      handleEditAjax(params,callback){
        let _params = {...params}
        _params.pid = this.obj.pid;
        _params.id = this.obj.id;
        WareOrderTypeEdit(_params).then(()=>{
          callback(true)
        }).catch(()=>{
          callback(false)
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.$emit('update:visible',false)
      }
    }
  }
</script>
<style lang="less" scoped>
.order-type-modal-content{
  padding: 20px 16px 0 16px;
}
</style>
